<template>
  <el-form ref="form" :model="formModal" :rules="rules" label-width="80px">
    <el-form-item label="公司名称" prop="comUsername">
      <el-input v-model="formModal.comUsername" maxlength="30" />
    </el-form-item>
    <el-form-item label="联系方式" prop="comUsertele">
      <el-input v-model="formModal.comUsertele" maxlength="11" />
    </el-form-item>
    <el-form-item label="邮箱地址" prop="comUsermail">
      <el-input v-model="formModal.comUsermail" maxlength="50" />
    </el-form-item>
    <el-form-item label="企业简介" prop="comUserbrief">
      <el-input type="textarea" v-model="formModal.comUserbrief" maxlength="100" />
    </el-form-item>
    <el-form-item label="联系地址" prop="comUseraddr">
      <el-input type="textarea" v-model="formModal.comUseraddr" maxlength="50" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">修改</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateComhome } from "@/api/truth/comhome";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      // 表单校验
      formModal: {},
      rules: {
        comUsername: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        comUsermail: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        comUsertele: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^0\d{2,3}-\d{6,8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        comUserbrief: [
          { required: true, message: "公司简介不能为空", trigger: "blur" }
        ],
        comUseraddr: [
          { required: true, message: "联系地址不能为空", trigger: "blur" }
        ],
      }
    };

  },
  watch: {
    user(val) {
      console.log(val);
      setTimeout(() => {
        this.formModal = val
      }, 10);
      console.log(val);
    }
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateComhome(this.user).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>
